home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 7
/
Apprentice-Release7.iso
/
Environments
/
PowerLisp 2.01
/
Supplemental Documentation
/
Documentation
/
Chapter 01. Introduction
< prev
next >
Wrap
Text File
|
1995-03-27
|
27KB
|
574 lines
Common Lisp the Language, 2nd Edition
-------------------------------------------------------------------------------
1. Introduction
Common Lisp is a new dialect of Lisp, a successor to MacLisp [33,37],
influenced strongly by Zetalisp [55,34] and to some extent by Scheme [46] and
Interlisp [50].
-------------------------------------------------------------------------------
* Purpose
* Notational Conventions
o Decimal Numbers
o Nil, False, and the Empty List
o Evaluation, Expansion, and Equivalence
o Errors
o Descriptions of Functions and Other Entities
o The Lisp Reader
o Overview of Syntax
-------------------------------------------------------------------------------
1.1. Purpose
Common Lisp is intended to meet these goals:
Commonality
Common Lisp originated in an attempt to focus the work of several
implementation groups, each of which was constructing successor
implementations of MacLisp for different computers. These implementations
had begun to diverge because of the differences in the implementation
environments: microcoded personal computers (Zetalisp, Spice Lisp),
commercial timeshared computers (NIL-the ``New Implementation of Lisp''),
and supercomputers (S-1 Lisp). While the differences among the several
implementation environments of necessity will continue to force certain
incompatibilities among the implementations, Common Lisp serves as a
common dialect to which each implementation makes any necessary
extensions.
Portability
Common Lisp intentionally excludes features that cannot be implemented
easily on a broad class of machines. On the one hand, features that are
difficult or expensive to implement on hardware without special microcode
are avoided or provided in a more abstract and efficiently implementable
form. (Examples of this are the invisible forwarding pointers and
locatives of Zetalisp. Some of the problems that they solve are addressed
in different ways in Common Lisp.) On the other hand, features that are
useful only on certain ``ordinary'' or ``commercial'' processors are
avoided or made optional. (An example of this is the type declaration
facility, which is useful in some implementations and completely ignored
in others. Type declarations are completely optional and for correct
programs affect only efficiency, not semantics.) Common Lisp is designed
to make it easy to write programs that depend as little as possible on
machine-specific characteristics, such as word length, while allowing some
variety of implementation techniques.
Consistency
Most Lisp implementations are internally inconsistent in that by default
the interpreter and compiler may assign different semantics to correct
programs. This semantic difference stems primarily from the fact that the
interpreter assumes all variables to be dynamically scoped, whereas the
compiler assumes all variables to be local unless explicitly directed
otherwise. This difference has been the usual practice in Lisp for the
sake of convenience and efficiency but can lead to very subtle bugs. The
definition of Common Lisp avoids such anomalies by explicitly requiring
the interpreter and compiler to impose identical semantics on correct
programs so far as possible.
Expressiveness
Common Lisp culls what experience has shown to be the most useful and
understandable constructs from not only MacLisp but also Interlisp, other
Lisp dialects, and other programming languages. Constructs judged to be
awkward or less useful have been excluded. (An example is the store
construct of MacLisp.)
Compatibility
Unless there is a good reason to the contrary, Common Lisp strives to be
compatible with Lisp Machine Lisp, MacLisp, and Interlisp, roughly in that
order.
Efficiency
Common Lisp has a number of features designed to facilitate the production
of high-quality compiled code in those implementations whose developers
care to invest effort in an optimizing compiler. One implementation of
Common Lisp, namely S-1 Lisp, already has a compiler that produces code
for numerical computations that is competitive in execution speed to that
produced by a Fortran compiler [11]. The S-1 Lisp compiler extends the
work done in MacLisp to produce extremely efficient numerical code [19].
Power
Common Lisp is a descendant of MacLisp, which has traditionally placed
emphasis on providing system-building tools. Such tools may in turn be
used to build the user-level packages such as Interlisp provides; these
packages are not, however, part of the Common Lisp core specification. It
is expected such packages will be built on top of the Common Lisp core.
Stability
It is intended that Common Lisp will change only slowly and with due
deliberation. The various dialects that are supersets of Common Lisp may
serve as laboratories within which to test language extensions, but such
extensions will be added to Common Lisp only after careful examination and
experimentation.
The goals of Common Lisp are thus very close to those of Standard Lisp [31] and
Portable Standard Lisp [51]. Common Lisp differs from Standard Lisp primarily
in incorporating more features, including a richer and more complicated set of
data types and more complex control structures.
This book is intended to be a language specification rather than an
implementation specification (although implementation notes are scattered
throughout the text). It defines a set of standard language concepts and
constructs that may be used for communication of data structures and algorithms
in the Common Lisp dialect. This set of concepts and constructs is sometimes
referred to as the ``core Common Lisp language'' because it contains
conceptually necessary or important features. It is not necessarily
implementationally minimal. While many features could be defined in terms of
others by writing Lisp code, and indeed may be implemented that way, it was
felt that these features should be conceptually primitive so that there might
be agreement among all users as to their usage. (For example, bignums and
rational numbers could be implemented as Lisp code given operations on fixnums.
However, it is important to the conceptual integrity of the language that they
be regarded by the user as primitive, and they are useful enough to warrant a
standard definition.)
For the most part, this book defines a programming language, not a programming
environment. A few interfaces are defined for invoking such standard
programming tools as a compiler, an editor, a program trace facility, and a
debugger, but very little is said about their nature or operation. It is
expected that one or more extensive programming environments will be built
using Common Lisp as a foundation, and will be documented separately.
[change_begin]
There are now many implementations of Common Lisp, some programmed by research
groups in universities and some by companies that sell them commercially, and a
number of useful programming environments have indeed grown up around these
implementations. What is more, all the goals stated above have been achieved,
most notably that of portability. Moving large bodies of Lisp code from one
computer to another is now routine.
[change_end]
1.2. Notational Conventions
A number of special notational conventions are used throughout this book for
the sake of conciseness.
-------------------------------------------------------------------------------
* Decimal Numbers
* Nil, False, and the Empty List
* Evaluation, Expansion, and Equivalence
* Errors
* Descriptions of Functions and Other Entities
* The Lisp Reader
* Overview of Syntax
-------------------------------------------------------------------------------
1.2.1. Decimal Numbers
All numbers in this book are in decimal notation unless there is an explicit
indication to the contrary. (Decimal notation is normally taken for granted, of
course. Unfortunately, for certain other dialects of Lisp, MacLisp in
particular, the default notation for numbers is octal (base 8) rather than
decimal, and so the use of decimal notation for describing Common Lisp is,
taken in its historical context, a bit unusual!)
1.2.2. Nil, False, and the Empty List
In Common Lisp, as in most Lisp dialects, the symbol nil is used to represent
both the empty list and the ``false'' value for Boolean tests. An empty list
may, of course, also be written (); this normally denotes the same object as
nil. (It is possible, by extremely perverse manipulation of the package system,
to cause the sequence of letters nil to be recognized not as the symbol that
represents the empty list but as another symbol with the same name. This
obscure possibility will be ignored in this book.) These two notations may be
used interchangeably as far as the Lisp system is concerned. However, as a
matter of style, this book uses the notation () when it is desirable to
emphasize the use of an empty list, and uses the notation nil when it is
desirable to emphasize the use of the Boolean ``false''. The notation 'nil
(note the explicit quotation mark) is used to emphasize the use of a symbol.
For example:
(defun three () 3) ;Emphasize empty parameter list
(append '() '()) => () ;Emphasize use of empty lists
(not nil) => t ;Emphasize use as Boolean ``false''
(get 'nil 'color) ;Emphasize use as a symbol
Any data object other than nil is construed to be Boolean ``not false'', that
is, ``true''. The symbol t is conventionally used to mean ``true'' when no
other value is more appropriate. When a function is said to ``return false'' or
to ``be false'' in some circumstance, this means that it returns nil. However,
when a function is said to ``return true'' or to ``be true'' in some
circumstance, this means that it returns some value other than nil, but not
necessarily t.
1.2.3. Evaluation, Expansion, and Equivalence
Execution of code in Lisp is called evaluation because executing a piece of
code normally results in a data object called the value produced by the code.
The symbol => is used in examples to indicate evaluation. For example,
(+ 4 5) => 9
means ``the result of evaluating the code (+ 4 5) is (or would be, or would
have been) 9.''
The symbol -> is used in examples to indicate macro expansion. For example,
(push x v) -> (setf v (cons x v))
means ``the result of expanding the macro-call form (push x v) is (setf v (cons
x v)).'' This implies that the two pieces of code do the same thing; the second
piece of code is the definition of what the first does.
The symbol == is used in examples to indicate code equivalence. For example,
(gcd x (gcd y z)) == (gcd (gcd x y) z)
means ``the value and effects of evaluating the form (gcd x (gcd y z)) are
always the same as the value and effects of (gcd (gcd x y) z) for any values of
the variables x, y, and z.'' This implies that the two pieces of code do the
same thing; however, neither directly defines the other in the way macro
expansion does.
1.2.4. Errors
When this book specifies that it ``is an error'' for some situation to occur,
this means that:
* No valid Common Lisp program should cause this situation to occur.
* If this situation occurs, the effects and results are completely
undefined as far as adherence to the Common Lisp specification is
concerned.
* No Common Lisp implementation is required to detect such an error. Of
course, implementors are encouraged to provide for detection of such
errors wherever reasonable.
This is not to say that some particular implementation might not define the
effects and results for such a situation; the point is that no program
conforming to the Common Lisp specification may correctly depend on such
effects or results.
On the other hand, if it is specified in this book that in some situation ``an
error is signaled,'' this means that:
* If this situation occurs, an error will be signaled (see error and
cerror).
* Valid Common Lisp programs may rely on the fact that an error will be
signaled.
* Every Common Lisp implementation is required to detect such an error.
In places where it is stated that so-and-so ``must'' or ``must not'' or ``may
not'' be the case, then it ``is an error'' if the stated requirement is not
met. For example, if an argument ``must be a symbol,'' then it ``is an error''
if the argument is not a symbol. In all cases where an error is to be signaled,
the word ``signaled'' is always used explicitly in this book.
[change_begin]
X3J13 has adopted a more elaborate terminology for errors, and has made some
effort to specify the type of error to be signaled in situations where
signaling is appropriate. This effort was not complete as of September 1989,
and I have made little attempt to incorporate the new error terminology or
error type specifications in this book. However, the new terminology is
described and used in the specification of the Common Lisp Object System
appearing in chapter 28; this gives the flavor of how erroneous situations will
be described, and appropriate actions prescribed, in the forthcoming ANSI
Common Lisp standard.
[change_end]
1.2.5. Descriptions of Functions and Other Entities
Functions, variables, named constants, special forms, and macros are described
using a distinctive typographical format. Table 1-1 illustrates the manner in
which Common Lisp functions are documented. The first line specifies the name
of the function, the manner in which it accepts arguments, and the fact that it
is a function. If the function takes many arguments, then the names of the
arguments may spill across two or three lines. The paragraphs following this
standard header explain the definition and uses of the function and often
present examples or related functions.
Sometimes two or more related functions are explained in a single combined
description. In this situation the headers for all the functions appear
together, followed by the combined description.
In general, actual code (including actual names of functions) appears in this
typeface: (cons a b). Names that stand for pieces of code (metavariables) are
written in italics. In a function description, the names of the parameters
appear in italics for expository purposes. The word &optional in the list of
parameters indicates that all arguments past that point are optional; the
default values for the parameters are described in the text. Parameter lists
may also contain &rest, indicating that an indefinite number of arguments may
appear, or &key, indicating that keyword arguments are accepted. (The
&optional/&rest/&key syntax is actually used in Common Lisp function
definitions for these purposes.)
----------------------------------------------------------------
Table 1-1: Sample Function Description
[Function]
sample-function arg1 arg2 &optional arg3 arg4
The function sample-function adds together arg1 and arg2,
and then multiplies the result by arg3. If arg3 is not
provided or is nil, the multiplication isn't done.
sample-function then returns a list whose first element is
this result and whose section element is arg4 (which
defaults to the symbol foo). For example:
(sample-function 3 4) => (7 foo)
(sample-function 1 2 2 'bar) => (6 bar)
In general, (sample-function x y) == (list (+ x y) 'foo).
----------------------------------------------------------------
----------------------------------------------------------------
Table 1-2: Sample Variable Description
[Variable]
*sample-variable*
The variable *sample-variable* specifies how many times the
special form sample-special-form should iterate. The value
should always be a non-negative integer or nil (which means
iterate indefinitely many times). The initial value is 0
(meaning no iterations).
----------------------------------------------------------------
----------------------------------------------------------------
Table 1-3: Sample Constant Description
[Constant]
sample-constant
The named constant sample-constant has as its value the
height of the terminal screen in furlongs times the base-2
logarithm of the implementation's total disk capacity in
bytes, as a floating-point number.
----------------------------------------------------------------
Table 1-2 illustrates the manner in which a global variable is documented. The
first line specifies the name of the variable and the fact that it is a
variable. Purely as a matter of convention, all global variables used by Common
Lisp have names beginning and ending with an asterisk.
Table 1-3 illustrates the manner in which a named constant is documented. The
first line specifies the name of the constant and the fact that it is a
constant. (A constant is just like a global variable, except that it is an
error ever to alter its value or to bind it to a new value.)
----------------------------------------------------------------
Table 1-4: Sample Special Form Description
[Special Form]
sample-special-form [name] ({var}*) {form}+
This evaluates each form in sequence as an implicit progn,
and does this as many times as specified by the global
variable *sample-variable*. Each variable var is
bound and initialized to 43 before the first iteration, and
unbound after the last iteration. The name name, if
supplied, may be used in a return-from form to exit from the
loop prematurely. If the loop ends normally,
sample-special-form returns nil. For example:
(setq *sample-variable* 3)
(sample-special-form () form1 form2)
This evaluates form1, form2, form1, form2, form1, form2,
in that order.
----------------------------------------------------------------
----------------------------------------------------------------
Table 1-5: Sample Macro Description
[Macro]
sample-macro var [[ declaration* | doc-string ]] {tag | statement}*
This evaluates the statements as a prog body, with the
variable var bound to 43.
(sample-macro x (return (+ x x))) => 86
(sample-macro var . body) -> (prog ((var 43)) . body)
----------------------------------------------------------------
Tables 1-4 and 1-5 illustrate the documentation of special forms and macros,
which are closely related in purpose. These are very different from functions.
Functions are called according to a single, specific, consistent syntax; the
&optional/&rest/&key syntax specifies how the function uses its arguments
internally but does not affect the syntax of a call. In contrast, each special
form or macro can have its own idiosyncratic syntax. It is by special forms and
macros that the syntax of Common Lisp is defined and extended.
In the description of a special form or macro, an italicized word names a
corresponding part of the form that invokes the special form or macro.
Parentheses stand for themselves and should be written as such when invoking
the special form or macro. Brackets, braces, stars, plus signs, and vertical
bars are metasyntactic marks. Brackets, [ and ], indicate that what they
enclose is optional (may appear zero times or one time in that place); the
square brackets should not be written in code. Braces, { and }, simply
parenthesize what they enclose but may be followed by a star, *, or a plus
sign, +; a star indicates that what the braces enclose may appear any number of
times (including zero, that is, not at all), whereas a plus sign indicates that
what the braces enclose may appear any non-zero number of times (that is, must
appear at least once). Within braces or brackets, a vertical bar, |, separates
mutually exclusive choices. In summary, the notation {x}* means zero or more
occurrences of x, the notation {x}+ means one or more occurrences of x, and the
notation [x] means zero or one occurrence of x. These notations are also used
for syntactic descriptions expressed as BNF-like productions, as in table 22-2.
[change_begin]
Double brackets, [[ and ]], indicate that any number of the alternatives
enclosed may be used, and those used may occur in any order, but each
alternative may be used at most once unless followed by a star. For example,
p [[x | {y}* | z]] q
means that at most one x, any number of y's, and at most one z may appear
between the mandatory occurrences of p and q, and those that appear may be in
any order.
A downward arrow, [?], indicates a form of syntactic indirection that helps to
make
[[ ]] notation more readable. If X is some non-terminal symbol occurring on the
left-hand side of some BNF production, then the right-hand side of that
production is to be textually substituted for any occurrence of [?]X. Thus the
two fragments
p [[[?]xyz-mixture]] q
xyz-mixture ::= x | {y}* | z
are together equivalent to the previous example.
[change_end]
In the last example in table 1-5, notice the use of dot notation. The dot
appearing in the expression (sample-macro var . body) means that the name body
stands for a list of forms, not just a single form, at the end of a list. This
notation is often used in examples.
[change_begin]
In the heading line in table 1-5, notice the use of [[ ]] notation to indicate
that any number of declarations may appear but at most one documentation string
(which may appear before, after, or somewhere in the middle of any
declarations).
[change_end]
1.2.6 The Lisp Reader
The term ``Lisp reader'' refers not to you, the reader of this book, nor to
some person reading Lisp code, but specifically to a Lisp procedure, namely the
function read, which reads characters from an input stream and interprets them
by parsing as representations of Lisp objects.
1.2.7. Overview of Syntax
Certain characters are used in special ways in the syntax of Common Lisp. The
complete syntax is explained in detail in chapter 22, but a quick summary here
may be useful:
( A left parenthesis begins a list of items. The list may contain any number
of items, including zero. Lists may be nested. For example, (cons (car x)
(cdr y)) is a list of three things, of which the last two are themselves
lists.
) A right parenthesis ends a list of items.
' An acute accent (also called single quote or apostrophe) followed by an
expression form is an abbreviation for (quote form). Thus 'foo means
(quote foo) and '(cons 'a 'b) means (quote (cons (quote a) (quote b))).
; Semicolon is the comment character. It and all characters up to the end of
the line are discarded.
" Double quotes surround character strings:
"This is a thirty-nine-character string."
\ Backslash is an escape character. It causes the next character to be
treated as a letter rather than for its usual syntactic purpose. For
example, A\(B denotes a symbol whose name consists of the three characters
A, (, and B. Similarly, "\"" denotes a character string containing one
character, a double quote, because the first and third double quotes serve
to delimit the string, and the second double quote serves as the contents
of the string. The backslash causes the second double quote to be taken
literally and prevents it from being interpreted as the terminating
delimiter of the string.
| Vertical bars are used in pairs to surround the name (or part of the name)
of a symbol that has many special characters in it. It is roughly
equivalent to putting a backslash in front of every character so
surrounded. For example, |A(B)|, A|(|B|)|, and A\(B\) all mean the symbol
whose name consists of the four characters A, (, B, and ).
# The number sign signals the beginning of a complicated syntactic
structure. The next character designates the precise syntax to follow. For
example, #o105 means (105 in octal notation); #x105 means (105 in
hexadecimal notation); #b1011 means (1011 in binary notation); #\L
denotes a character object for the character L; and #(a b c) denotes a
vector of three elements a, b, and c. A particularly important case is
that #'fn means (function fn), in a manner analogous to 'form meaning
(quote form).
` Grave accent (``backquote'') signals that the next expression is a
template that may contain commas. The backquote syntax represents a
program that will construct a data structure according to the template.
, Commas are used within the backquote syntax.
: Colon is used to indicate which package a symbol belongs to. For example,
network:reset denotes the symbol named reset in the package named network.
A leading colon indicates a keyword, a symbol that always evaluates to
itself. The colon character is not actually part of the print name of the
symbol. This is all explained in chapter 11; until you read that, just
keep in mind that a symbol notated with a leading colon is in effect a
constant that evaluates to itself.
[change_begin]
Notice of correction. In the first edition, the characters ``,'' and ``:'' at
the left margin above were inadvertently omitted.
[change_end]
Brackets, braces, question mark, and exclamation point (that is, [, ], {, }, ?,
and !) are not used for any purpose in standard Common Lisp syntax. These
characters are explicitly reserved to the user, primarily for use as macro
characters for user-defined lexical syntax extensions (see section 22.1.3).
[old_change_begin]
All code in this book is written using lowercase letters. Common Lisp is
generally insensitive to the case in which code is written. Internally, names
of symbols are ordinarily converted to and stored in uppercase form. There are
ways to force case conversion on output if desired; see *print-case*. In this
book, wherever an interactive exchange between a user and the Lisp system is
shown, the input is exhibited with lowercase letters and the output with
uppercase letters.
[old_change_end]
[change_begin]
X3J13 voted in June 1989 (READ-CASE-SENSITIVITY) to introduce readtable-case.
Certain settings allow the names of symbols to be case-sensitive. The default
behavior, however, is as described in the previous paragraph. In any event,
only uppercase letters appear in the internal print names of symbols naming the
standard Common Lisp facilities described in this book.
[change_end]